﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GoldBoxIconComponent : MonoBehaviour
{
    int TypeNum = 0;
    List<GoldType> TypeList = new List<GoldType>();
    GoldType type;
    Image image;
    int breakSum = 0;
    bool IsBreak = false;
    KeyCode IconKey;
    List<KeyCode> keyCodesList = new List<KeyCode>();
    private void OnEnable()
    {
        TypeNum = UnityEngine.Random.Range(4, 6);
        image = GetComponent<Image>();
        CreateIcon();
        image.sprite = Resources.Load<Sprite>("GlodType/" + TypeList[breakSum].ToString());
        IconKey = keyCodesList[breakSum];
    } 

    public void IconBreak(KeyCode key)
    {
        if (key == IconKey)
        {
            NextIcon();
        }
    }

    void NextIcon()
    {
        GetComponent<Animation>().Play();
        if (breakSum < TypeList.Count-1)
        {
            breakSum++;
            image.sprite = Resources.Load<Sprite>("GlodType/" + TypeList[breakSum].ToString());
            IconKey = keyCodesList[breakSum];
        }
        else
        {
            IsBreak = true;
        }
    }

    public bool GetGlodBreak()
    {
        return IsBreak;
    }
    private void OnDisable()
    {
        IsBreak = false;
        TypeList.Clear();
        keyCodesList.Clear();
        breakSum = 0;
    }

    void CreateIcon()
    {
        for (int i = 0; i < TypeNum; i++)
        {
            RandomType();
        }
    }
    void RandomType()
    {
        type = (GoldType)UnityEngine.Random.Range(0, 7);
        while (true)
        {
            if (inspectList())
            {
                if (TypeList.Count >= 8)
                {
                    TypeList.Clear();
                    keyCodesList.Clear();
                }
                break;
            }
            else
            {
                type = (GoldType)UnityEngine.Random.Range(0, 7);
            }
        }
    }
    bool inspectList()
    {
        foreach (GoldType curType in TypeList)
        {
            if (type == curType || type.ToString().Equals(curType.ToString()))
            {
                return false;
            }
        }
        TypeList.Add(type);
        keyCodesList.Add(GetKey());
        return true;
    }

    KeyCode GetKey()
    {
        KeyCode key = KeyCode.UpArrow;
        switch (type)
        {
            case 0:
                key = KeyCode.UpArrow;
                break;
            case (GoldType)1:
                key = KeyCode.DownArrow;
                break;
            case (GoldType)2:
                key = KeyCode.LeftArrow;
                break;
            case (GoldType)3:
                key = KeyCode.RightArrow;
                break;
            case (GoldType)4:
                key = KeyCode.Keypad7;
                break;
            case (GoldType)5:
                key = KeyCode.Keypad2;
                break;
            case (GoldType)6:
                key = KeyCode.Keypad3;
                break;
        }
        return key;
    }
}
